package com.example.test_01.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test_01.R;
import com.example.test_01.adapter.FoodAdapter;
import com.example.test_01.adapter.FoodOrderAdapter;
import com.example.test_01.adapter.FoodSpinnerAdapter;
import com.example.test_01.adapter.UserAdapter;
import com.example.test_01.adapter.UserSpinnerAdapter;
import com.example.test_01.model.Food;
import com.example.test_01.model.Orders;
import com.example.test_01.model.User;
import com.example.test_01.service.FoodService;
import com.example.test_01.service.FoodServiceImpl;
import com.example.test_01.service.FoodTypeService;
import com.example.test_01.service.FoodTypeServiceImpl;
import com.example.test_01.service.OrderService;
import com.example.test_01.service.OrderServiceImpl;
import com.example.test_01.service.UserService;
import com.example.test_01.service.UserServiceImpl;
import com.example.test_01.utils.Result;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderList extends AppCompatActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener {
    private static final String TAG = "OrderList";

    private final OrderService orderService = new OrderServiceImpl();
    private final UserService userService = new UserServiceImpl();
    private final FoodService foodService = new FoodServiceImpl();
    private final FoodTypeService typeService = new FoodTypeServiceImpl();

    // 菜品
    public Food food;
    public List<Food> foodList = new ArrayList<Food>();

    //    配置FoodType spinner
    private List<String> typeNames;
    private ArrayAdapter<String> typeNameAdapter;

    //    配置User Spinner
    private List<User> userList;
    private User user;
    private UserSpinnerAdapter userAdapter;

    //    配置展示用Food Spinner
    FoodOrderAdapter adapter;
    //    控件
    Button btn_order_finish;
    Button btn_order_add_food;
    Button btn_order_create;

    AlertDialog.Builder builder;
    TextView text_price_sum;
    EditText input_order_customer_num;
    EditText input_order_table_num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
//        配置User
        userList = userService.list();
        userAdapter = new UserSpinnerAdapter(this,userList);
        Spinner spinner_order_user = findViewById(R.id.spinner_order_user);
        spinner_order_user.setAdapter(userAdapter);
        spinner_order_user.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                user = userAdapter.getUserItem(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        input_order_customer_num = findViewById(R.id.input_order_customer_num);
        input_order_table_num = findViewById(R.id.input_order_table_num);

        btn_order_add_food = findViewById(R.id.btn_order_add_food);
        btn_order_add_food.setOnClickListener(this);

        btn_order_finish = findViewById(R.id.btn_order_finish);
        btn_order_finish.setOnClickListener(this);


        btn_order_create = findViewById(R.id.btn_order_create);
        btn_order_create.setOnClickListener(this);

//        配置foodList展示
        RecyclerView recyclerView = findViewById(R.id.rv_order_food_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new FoodOrderAdapter(foodList);
        recyclerView.setAdapter(adapter);

        text_price_sum = findViewById(R.id.text_price_sum);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this,"你选择的是" + userList.get(i).getUsername(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_order_add_food:{

                View view2 = View.inflate(this,R.layout.order_form,null);
                // 单价
                TextView priceText = view2.findViewById(R.id.text_food_price);
                //                配置菜品下拉框
                FoodSpinnerAdapter foodSpinnerAdapter = new FoodSpinnerAdapter(this);
                final Spinner spinner_food = view2.findViewById(R.id.spinner_food);
                spinner_food.setAdapter(foodSpinnerAdapter);
                spinner_food.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        food = foodSpinnerAdapter.getItemFood(i);
                        priceText.setText(food.getPrice().toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {
                        priceText.setText("");
                    }
                });

                //                配置菜品分类下拉框
                typeNames = typeService.GetAllName();
                typeNameAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,typeNames);
                final Spinner spinner_food_type = view2.findViewById(R.id.spinner_food_type);
                spinner_food_type.setAdapter(typeNameAdapter);
                spinner_food_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        foodSpinnerAdapter.reLoad(typeNames.get(i));
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

                builder = new AlertDialog.Builder(this);
                builder.setTitle("点菜").setView(view2);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        foodList.add(food);
                        adapter.reload(foodList);
                        text_price_sum.setText(adapter.getSum() + "￥");
                    }
                });
                builder.create().show();
                break;
            }
            case R.id.btn_order_finish:{
                Orders orders = new Orders();
                orders.setCreateTime(new Date());
                orders.setSum(adapter.getSum());
                orders.setCustomerNum(Integer.parseInt(input_order_customer_num.getText().toString()));
                orders.setSeatNo(Integer.parseInt(input_order_table_num.getText().toString()));
                orders.setUserId(user.getId());
                orderService.Add(orders);
                Toast.makeText(this,"结账成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
            case R.id.btn_order_create:{
                // 发送自定义广播，后厨助手程序接收
                Intent intent = new Intent();
                intent.setAction("com.example.order");
                String foodListNames = "";
                for(int i = 0; i < foodList.size(); i++){
                    foodListNames += foodList.get(i).getName() + "  ";
                }
                intent.putExtra("foodList",foodListNames);
                intent.putExtra("createTime",new Date().toString());
                sendBroadcast(intent);
            }
            break;
        }
    }
}